'''
Created on 06/06/2011

@author: Adam
'''
from flask import request
from jsonGql import jsonify

from application.models import Map
from application.decorators import login_required
from google.appengine.ext import db

@login_required
def mapRegion():
	limit = request.args.get('limit', 250, type=int)
	offset = request.args.get('offset', 0, type=int)
		
	results = Map.Region.all().order('name').fetch(limit, offset)
	return jsonify(results)

@login_required
def mapConstellation():
	limit = request.args.get('limit', 250, type=int)
	offset = request.args.get('offset', 0, type=int)
	region = request.args.get('region', None, type=str)
	
	query = Map.Constellation.all()
	if region is not None:
		query.filter('region = ', db.Key(region))
	results = query.order('name').fetch(limit, offset)
	return jsonify(results)

@login_required
def mapSolarSystem():
	limit = request.args.get('limit', 250, type=int)
	offset = request.args.get('offset', 0, type=int)
	constellation = request.args.get('constellation', None, type=str)
	
	query = Map.SolarSystem.all()
	if constellation is not None:
		query.filter('constellation = ', db.Key(constellation))
	results = query.order('name').fetch(limit, offset)
	return jsonify(results)

@login_required
def mapMoon():
	limit = request.args.get('limit', 250, type=int)
	offset = request.args.get('offset', 0, type=int)
	solarSystem = request.args.get('solarSystem', None, type=str)
	
	query = Map.Moon.all()
	if solarSystem is not None:
		query.filter('solarSystem = ', db.Key(solarSystem))	
	results = query.order('name').fetch(limit, offset)
	return jsonify(results)